var token=localStorage.getItem("key");
//调取地址收货列表
$.ajax({
	type:"get",
	url:"/api/me/getShippingAddress",
	async: true,
	data: {"token":token},
	dataType: "json",
	success:function(data){
		console.log(data);
		var list = data.data;
//		var str="";
		if(list!='[]'&&list.length>0){
			for (var i=0;i<list.length;i++) {
				str='<li class="user-addresslist '+(list[i].beDefault==0?'':'defaultAddr')+'">'+
						'<span class="new-option-r"><i class="am-icon-check-circle"></i>默认地址</span>'+
						'<div class="new-tit new-p-re">'+
							'<span class="new-txt">'+list[i].recipient+'</span>&nbsp;'+
							'<span class="new-txt-rd2">'+list[i].phone+'</span>'+
						'</div>'+
						'<div class="new-mu_l2a new-p-re">'+
							'<p class="new-mu_l2cw">'+
								'<span class="title">地址：</span>'+
						   		'<span class="province">'+list[i].province+'</span>'+
								'<span class="city">'+list[i].city+'</span>'+
								'<span class="dist">'+list[i].district+'</span><br/>'+
								'<span class="street">'+list[i].address+'</span>'+
							'</p>'+
						'</div>'+
						'<div class="new-addr-btn">'+
							'<a href="javascript:void(0);" data-set="'+list[i].addressId+'" class="compile"><i class="am-icon-edit"></i>编辑</a>'+
							'<span class="new-addr-bar">|</span>'+
							'<a href="javascript:void(0);" onclick="deladd(\''+list[i].addressId+'\')"><i class="am-icon-trash"></i>删除</a>'+
						'</div>'+
					'</li>';
				
				$(".am-thumbnails").append(str);
//				$('.user-addresslist').addClass("defaultAddr"+list[i].beDefault);
//				$(".new-option-r").click(function() {
//					$(this).parent('.user-addresslist').addClass("defaultAddr").siblings().removeClass("defaultAddr");
//				});
			}
			
		}
	}
});



$(function(){
	//是否设置为默认地址
	var addressId="";
	var beDefault="0";
	$('input[type=checkbox]').click( function () {
        if ($(this).attr('checked') == "checked") {
        	beDefault="1";
//      	console.log(beDefault);
    		console.log(beDefault);
    	}
	});
	
	//	新增地址
	$(document).on("click","#save_add",function(){
		var phone=$('#user-phone').val();
		var recipient = $('#user-name').val();
		var province=$('.prov').find("option:selected").text();
		var city=$(".city").find("option:selected").text();
		var district=$(".dist").find("option:selected").text();
		var address=$('#user-intro').val();
//		alert(recipient);
		//alert(addressId);
		//alert(typeof addressId);
		addressId=addressId+'';
		if(addressId == ""){
			$.ajax({
				type: "get",
				url: "/api/me/addOrUpdateAddress",
				async: true,
				data: {"token":token,"phone":phone,"beDefault":beDefault,"recipient":recipient,"province":province,"city":city,"district":district,"address":address},
				dataType: "json",
				success:function(data){
					window.location.reload();
				}
			});
		}else{
			$.ajax({
				type: "get",
				url: "/api/me/addOrUpdateAddress",
				async: true,
				data: {"token":token,"phone":phone,"beDefault":beDefault,"recipient":recipient,"province":province,"city":city,"district":district,"address":address,"addressId":addressId},
				dataType: "json",
				success:function(data){
					console.log(data);
					window.location.reload();
				}
			});
		}
		
	})
	
	//修改地址
	
	$(document).on("click",".compile",function(){
		addressId=($(this).data("set"));
		phone1=$(this).parent(".new-addr-btn").prev(".new-mu_l2a").prev(".new-tit").children(".new-txt-rd2").html();
		recipient1 = $(this).parent(".new-addr-btn").prev(".new-mu_l2a").prev(".new-tit").children(".new-txt").html();
		province1= $(this).parent(".new-addr-btn").prev(".new-mu_l2a").find(".province").html();
		city1=$(this).parent(".new-addr-btn").prev(".new-mu_l2a").find(".city").html();
		district1=$(this).parent(".new-addr-btn").prev(".new-mu_l2a").find(".dist").html();
		address1=$(this).parent(".new-addr-btn").prev(".new-mu_l2a").find(".street").html();
		$('#user-phone').val(phone1);
		$('#user-name').val(recipient1);
		$('.prov').find("option:selected").text(province1);
		$(".city").find("option:selected").text(city1);
		$(".dist").find("option:selected").text(district1);
		$('#user-intro').val(address1);
		
	})
	
})

//删除地址
function deladd(id){
	var isDel = window.confirm("确定删除地址？");
	if(isDel){
		$.ajax({
			type:"get",
			url:"/api/me/deleteShippingAddress",
			async:true,
			data:{"token":token,"addressId":id},
			dataType:"json",
			success:function(data){
				window.location.reload();
			}
		});
	}
	
}

